var appPath = getAppPath();	

$(function(){
	
	// 大分类
	$("#queryTmplType").combobox({
		url:appPath+'/itbTg/getTypeForCombo.action?topIds=1,2,3,4,6,7,8,1010,1011&blkFlg=1',
		valueField:'type_id',
		textField:'type_name',
		onSelect:function(selRecd){
			$("#queryTmplType2").combobox("clear");
			$("#queryTmplType3").combobox("clear");
			$("#queryTmplType2").combobox("loadData",{});
			$("#queryTmplType3").combobox("loadData",{});
			
			// 中分类
			var parentId=selRecd.type_id;
			if(Number(parentId)>0){
				$("#queryTmplType2").combobox({
					url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId+'&blkFlg=1',
					valueField:'type_id',
					textField:'type_name',
					onSelect:function(selRecd2){
						$("#queryTmplType3").combobox("clear");
						$("#queryTmplType3").combobox("loadData",{});
						
						// 小分类
						var parentId2=selRecd2.type_id;
						if(Number(parentId2)>0){
							$("#queryTmplType3").combobox({
								url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId2+'&blkFlg=1',
								valueField:'type_id',
								textField:'type_name',
								onSelect:function(selRecd3){
									// TODO
								}
							});
						}
					}
				});
			}
		}
	});
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbTg/getOutlineTmplList.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:false,
	    checkOnSelect:false,
	    columns:[[
	        {field:'fast',title:'区分',width:40,align:'left',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(Number(rowData.fast||'0') == 1) return '<span style="color:red">快速创建</span>';
		    		  return '<span style="color:green">普通实例</span>';
		    	  }
		      },
	        {field:'tg_id',title:'提纲ID',width:40,align:'left',sortable:false},
	        {field:'base_tg_id',title:'模版ID',width:40,align:'left',sortable:false,
		    	  formatter:function(baseId, rowData, rowIndex){
		    		  return '<span style="color:black">'+baseId+'</span>';
		    	  }
		      },
	        {field:'order',title:'显示顺序',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.tg_id) return "无";
		    		  return rowData.order;
		    	  }
		      },
		    {field:'top_type',title:'主分类',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var tn = '<span style="color:red">未知</span>';
		    		  switch(Number(value||'0')){
		    		  	case 1: tn='单页';break;
		    		  	case 2: tn='多页';break;
		    		  	case 3: tn='PPT';break;
		    		  	case 4: tn='印品';break;
		    		  	case 6: tn='社交媒体';break;
		    		  	case 7: tn='电商';break;
		    		  	case 8: tn='微商';break;
		    		  	case 1010: tn='LOGO商品';break;
		    		  	case 1011: tn='营销商品';break;
		    		  }
		    		  return tn;
		    	  }
		      },
	        {field:'editType',title:'提纲分类',width:60,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return rowData.type_name || '<span style="color:red">未分类</span>';
		    	  }
		      },
	        {field:'tg_name',title:'提纲名称',width:100,align:'left',sortable:false},
	        {field:'publish',title:'制作状态',width:45,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '已完成';
	        		return '制作中';
	        }}, 
	        {field:'num',title:'用户文档数',width:40,align:'center',sortable:false},
        	{field:'make',title:'用户文档信息',width:55,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return "<a href='javascript:void(0)' onclick='downStats("+rowData.num+","+rowData.tg_id+")'>导出</a>"
//	        		var url=appPath+"/itbTgWeb/downDocStats.action?tg_id="+rowData.tg_id;
//	        		return "<a href="+url+">导出</a>";
	        	}},
	        {field:'create_time',title:'创建时间',width:75,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:75,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true
	});

}

function downStats(num,tgId){
	if(num==0){
		$.messager.alert('提示信息','暂无用户信息下载！','info');
	}else{
			$.messager.confirm("确定","是否确认导出？",function(r){
			if(r){
				if(tgId!=null){
					$.loading(true);
					var downUrl=appPath+"/itbTgWeb/downDocStats.action?tg_id="+tgId;
					$.post(downUrl,
							function(ret){
								$.loading(false);
								if(ret && ret.length>0) {
								var strDate=$.DateUtil.format(new Date(),'yyyyMMddhhmmss');
								var fNm = "TG_"+tgId+"_UserDocInfo_" + strDate + ".csv";
								var blob = new Blob([ret]);
								var aLink = $('#downA');
								aLink.html(fNm);
								aLink.attr("download", fNm);
								aLink.attr("href", URL.createObjectURL(blob));//点击下载地址
								$("#downBtn").show();
								$('#downDlg').window({
											title:'用户文档信息',
											iconCls:'icon-save',
											modal: true,
											shadow: true,
											collapsible:false,
											minimizable:false,
											maximizable:false
										});
										$('#downDlg').window('move',{top:20,left:20});
										$('#downDlg').window('open');
						} else {
								$.messager.alert('提示信息','数据获取失败','info');
							}
					});
				}else{
					$.messager("参数错误，请联系管理员！",'info');
				}
			}
		});
	}
}
function cancelDown(){
	$('#downDlg').window('close');
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findTmplType =$("#queryTmplType3").combobox('getValue');
	if(!findTmplType){
		findTmplType =$("#queryTmplType2").combobox('getValue');
		if(!findTmplType){
			findTmplType=$("#queryTmplType").combobox('getValue');
		}
	}
					
	var findPubFlg = $("#queryPublish").val();
	var findTmplName=$("#queryTmplName").val();
	
	if (findTmplType) {
		queryObject.findTmplType = findTmplType;// 类型
	}
	if(findPubFlg&&findPubFlg.length>0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 样版名称
	queryObject.findMstFlg = 0;// 检索提纲实例
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(startFlg){
	$("#tmplGrid").datagrid('unselectAll');
	if(startFlg==1){
		$("#tmplGrid").datagrid('load', getQueryParam());
	} else {
		$("#tmplGrid").datagrid('reload', getQueryParam());
	}
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
